﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Collections;
using IBatisNet.Common.Utilities;
using System.Text;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;

namespace ExpertMIS.Service
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SearchQuestion : IHttpHandler
    {
        private int _titleLong = 30;
        private DomainBroker _domainBroker = new DomainBroker();
        private QuestionBroker _questionBroker = new QuestionBroker();
        private ADMUserBroker _userBroker = new ADMUserBroker();

        private string RefreshDomain(HttpRequest Request, string Keywords, string filter)
        {
            PagingSize pagingSize = new PagingSize();
            StringBuilder sbRet = new StringBuilder();

            int start = 0;
            if (!int.TryParse(Request["start"], out start))
            {
                start = -1;
            }
            else if (start <= 0)
            {
                start = 1;
            }

            int limit = 0;
            if (!int.TryParse(Request["limit"], out limit))
            {
                limit = -1;
            }
            else if (limit <= 0)
            {
                limit = 10;
            }

            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("domainquestion.length"), out _titleLong))
            {
                _titleLong = 30;
            }

            sbRet.Append("<tr class='result-head'><td class='col-title'>标&nbsp;题</td><td class='col-poster'>提问人</td><td class='col-date'>时间</td><td class='col-status'>状态</td></tr>");

            if (start > 0 || limit > 0)
            {
                DataTable questions = null;

                pagingSize.Start = start;
                pagingSize.Limit = limit;
                pagingSize.SortExpression = "F_UPDATEDATE DESC, F_CREATEDATE DESC";
                pagingSize.SearchKey = "";

                if ("".Equals(filter) || "all".Equals(filter))
                {
                    pagingSize.SearchKey = string.Empty;
                }
                else if ("resolved".Equals(filter))
                {
                    pagingSize.SearchKey = "F_Status = 3";
                }
                else if ("unsolved".Equals(filter))
                {
                    pagingSize.SearchKey = "F_Status <> 3";
                }
                questions = _questionBroker.SearchQuestionsByKeywords(Keywords, pagingSize);

                foreach (DataRowView row in questions.DefaultView)
                {
                    sbRet.Append("<tr>");

                    sbRet.Append("<td>");
                    if (!string.IsNullOrEmpty(Request["showdomain"]))
                    {
                        IList<DomainEntity> domains = _domainBroker.GetDomainsByQuerytionOID(row["F_OID"].ToString());
                        if (domains.Count > 0)
                        {
                            sbRet.Append(string.Format("<a href='DomainIntroduce.aspx?oid={0}' target='_self'>【{1}】</a>", domains[0].OID, domains[0].DomainName));
                        }
                    }
                    sbRet.Append(string.Format("<a href='QuestionForm.aspx?oid={1}' target='_blank'>{0}</a>", ExpertMIS.Codes.Utility.CutLongText(row["F_Title"], _titleLong), row["F_OID"]));
                    sbRet.Append("</td>");

                    ADMUserEntity user = _userBroker.GetEntityByOID(row["F_UserOID"].ToString());
                    if (user != null)
                    {
                        sbRet.Append(string.Format("<td>{0}</td>", user.Name));
                    }
                    else
                    {
                        sbRet.Append("<td></td>");
                    }

                    sbRet.Append(string.Format("<td>{0:yy-MM-dd}</td>", row["F_UpdateDate"]));

                    sbRet.Append(string.Format("<td>{0}</td>", QuestionBroker.TranslateQuestionStatus((QuestionStatus)row["F_Status"])));

                    sbRet.Append("</tr>");
                }
            }

            return string.Format("<table cellpadding='0' cellspacing='0' id='result' pageno='{1}' pagecount='{2}' recordcount='{3}'>{0}</table>", sbRet.ToString(), pagingSize.Start, pagingSize.Total == 0 ? pagingSize.Total : (int)System.Math.Ceiling((double)pagingSize.Total / pagingSize.Limit), pagingSize.Total);
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(RefreshDomain(context.Request, context.Request["keyword"], context.Request["filter"]));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
